package app.server.project.main.service.service.rank;

import app.server.project.main.service.db.dao.user.UserRankDao;
import cn.hutool.core.date.LocalDateTimeUtil;
import lombok.AllArgsConstructor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;

@AllArgsConstructor
@Component
public class InviteDayRank extends CommonRank {

    private final UserRankDao userRankDao;

    @Override
    public void doSettlement() {
        this.userRankDao.reset(this.getRankId());
    }

    @Override
    public long getNextSettlementTime() {
        var now = LocalDateTime.now();
        var tommorrow = now.plusDays(1);
        tommorrow = LocalDateTimeUtil.beginOfDay(tommorrow);
        var day = Duration.between(now, tommorrow);
        return day.toSeconds();
    }

    @EventListener(ApplicationReadyEvent.class)
    public void initRank() {
        this.init(RankType.InviteDay);
    }


    @Scheduled(cron = "0 0 0 * * ?")
    public void cornTime() {
        this.beginSettlement();
    }
}
